package cn.tyf.dao;

import cn.tyf.domain.Admin;
import cn.tyf.domain.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.beans.IntrospectionException;
import java.util.List;

@Repository
public interface UserDao {
    /* 普通用户相关操作 start*/

    /**
     * 根据name和passwo查询，用于登录
     * @param name
     * @param password
     * @return
     */
    @Select("select * from user where name=#{param1} and password=#{param2};")
    User userLogin(String name, String password);

    @Insert("insert into user(rid,name,password,gender,flag) value(#{rid},#{name},#{password},#{gender},#{flag})")
    int addUser(User user);
    /**
     * 分页查询
     * @param start
     * @param pageSize
     * @return
     */
    List<User> findByPage(String rid, String name,Integer flag,Integer start, Integer pageSize);

    /**
     * 查询总记录数
     * @return
     */
    Integer findByCount(String rid,String name,Integer flag);

    /**
     * 根据id进行删除
     * @param id
     */
    @Delete("delete from user where id = #{id}")
    void deleteById(Integer id);

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Select("select * from user where id = #{id}")
    User findById(Integer id);

    /**
     * 根据id更新，name姓名、password密码、flag状态
     * @param name
     * @param password
     * @param flag
     * @param id
     * @return
     */
    @Update("UPDATE user set name=#{arg0},password=#{arg1},flag=#{arg2} WHERE id=#{arg3};")
    void updateById(String name, String password, Integer flag, Integer id);

    /**
     * 根据用户id更新用户权限
     * @param flag
     * @param id
     */
    @Update("update user set flag = #{arg0} where id=#{arg1}")
    void updateFlag(Integer flag,Integer id);

    @Select("select * from user where id = #{arg0} and password = #{arg1}")
    User findByPassword(Integer id,String password);

    @Select("select * from user;")
    List<User> findAll();
    /* 普通用户相关操作 end*/


    /* 管理员相关操作 start*/
    /**
     * 根据id查询管理员
     * @param id
     * @return
     */
    @Select("select * from admin where id = #{id}")
    Admin findByAdminId(Integer id);

    /**
     * 管理员登录
     * @param name
     * @param password
     * @return
     */
    @Select("select * from admin where name=#{param1} and password=#{param2};")
    Admin adminLogin(String name, String password);

    /**
     * 更新管理员密码
     * @param password
     * @param id
     */
    @Update("UPDATE admin set password=#{arg0} WHERE id=#{arg1};")
    void updateByAdminId(String password,Integer id);

    /**
     * 根据卡号查询
     * @param card
     * @return
     */
    @Select("select * from user where rid = #{card}")
    User findByRid(String card);

    /**
     * 查询管理员总记录数
     * @param name
     * @return
     */
    //@Select("select count(*) from admin")
    Integer findByAdminCount(String name);

    /**
     * 管理员分页查询
     * @param name
     * @param start
     * @param pageSize
     * @return
     */
    List<Admin> findByAdminPage(String name, int start, Integer pageSize);

    /**
     * 根据id删除管理员
     * @param aid
     */
    @Delete("delete from admin where id=#{aid}")
    void deleteByAdminId(Integer aid);

    /**
     * 添加管理员
     * @param name
     * @param gender
     * @param password
     * @param power
     */
    @Insert("insert into admin(name,gender,password,power) value(#{arg0},#{arg1},#{arg2},#{arg3})")
    void insertAdmin(String name,String gender,String password,Integer power);

    @Select("select * from admin where name=#{name}")
    Admin findbyAdminName(String name);

    @Select("select * from admin where id=#{arg0} and password=#{arg1}")
    Admin findByAdminPassword(Integer id, String password);
    /* 管理员相关操作 end*/

}
